/**
 * @class   wosh::BundleGeneric
 * @brief   Intermediate (still abstract) implementation of Bundle.
 *          Class provides some common methods and features required
 *          by wosh::Bundle interface.
 *          Usually bundles inherit this class instead of wosh::Bundle
 *
 *
 * If you want to replace the base class's function, you must implement everything yourself.
 * However, if you only want to extend the base class's functionality,
 * then you implement what you want and call the base class to obtain
 * the default behavior for any cases you do not want to handle.
 *
 * \see     wosh::Bundle
 * @see     \ref page_bundles
 ****************************************************************************
 * @version 0.8.013 $Id: BundleGeneric.h 2804 2010-07-27 04:39:07Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_BundleGeneric_H__
 #define __WOSH_Core_BundleGeneric_H__

 #include <woshDefs.h>
 #include <core/Bundle.h>
 #include <core/Method.h>
 #include <core/Request.h>
 #include <core/LogContext.h>
 #include <core/Thread.h>
 #include <core/BusConnector.h>
 #include <core/BundleRequirements.h>
 #include <core/PropertiesProvider.h>
 #include <core/InterfacesProvider.h>
 #include <core/MethodsProvider.h>
 #include <core/SecurityToken.h>


namespace wosh {

 #define _BundleGeneric_NAME			"BundleGeneric"

class BundleGenericWorker;
template <class T> class DataFieldCollector;

class BundleGeneric :	public Bundle,
						public virtual IPropertiesProviderListener,
						protected virtual IThreadListener,
						protected MethodRunner
 {
	WOSH_CLASSNAME(wosh::BundleGeneric)


	protected:
		/**
		 * @brief  Empty constructor.
		 * @param parent [in] the parent object, usually NULL, still not really used.
		 */
		BundleGeneric( wosh::Object* parent = NULL );
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~BundleGeneric();


/** @name Bundle methods
 * @{
 ******************************************************************************/
	public:
		virtual WRESULT bundleRestart();
		virtual WRESULT bundleDiagnostic();
		virtual WRESULT bundleSnapshotState( long id );

//@}
/** @name IBusListener implementation
 * @{
 ******************************************************************************/
	public:
		virtual void busMessage( const Message& message, const Bus* source );
//@}
/** @name IPropertiesProviderListener interface
 * @{
 ******************************************************************************/
 	protected:
		// usually we don't need to implement this, but some Bundles (or especially devices)
		// may optimize the reflesh of information inline (here)
		// NOTE that caller will execute this code, usually from another Thread, processing should be
		// very STABLE and FAST.
		virtual inline bool readingProperty( Property* , const PropertiesProvider*  ) { return true; }
		virtual bool updatingProperty( bool& do_update, const Variant& value_proposed, Property& property_current, const PropertiesProvider* source );
//		friend class wosh::PropertiesProvider;
//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		const InterfacesProvider* bundleInterfaces() const		{ return &Interfaces; }
		const BundleRequirements* bundleRequirements() const	{ return &Requirements; }
		const MethodsProvider* bundleMethods() const			{ return &Methods; }
		const PropertiesProvider* bundleProperties() const		{ return &Properties; }
		PropertiesProvider* bundleProperties()					{ return &Properties; }

		const SecurityTokenProvider* bundleCredentials() const	{ return Credentials; }

		static DataFieldCollector<Bundle>* getDataFields();

//@}
/** @name Thread helpers
 * @{
 ******************************************************************************/
	protected:
		bool bundleValidate_StartStop( Bundle::BUNDLE_STATE state );
		WRESULT start_SynchThread( Thread* thread, unsigned long timeout = 30000 );
		WRESULT stop_SynchThread( Thread* thread, unsigned long timeout = 60000 );
//@}
/** @name Thread Events (IThreadListener interface)
 * @{
 ******************************************************************************/
 	public:
		virtual void thread_event( Thread::THREAD_STATE thread_event, Thread* thread_source );
//@}
	protected:
		friend class wosh::BundleGenericWorker;

	protected:
		virtual void setName( const std::string& value, bool updateURI = false );
		virtual void setBundleState( Bundle::BUNDLE_STATE state, bool raise_event = true );
		virtual void setPermission( const Permission& value );
		virtual void setCredentials( SecurityTokenProvider* credentials );

		virtual SecurityToken* signMessage( Message* message, const SecurityToken* source_token = NULL ) const;

	private:
		Response* mmDoBundleStart( const Request* request );
		Response* mmDoBundleStop( const Request* request );
		Response* mmDoBundleRestart( const Request* request );

	protected:
		BusConnector BusCore;						///< connection to the wosh.Bus.Core 
		mutable LogContext Log;							///< my logger

		mutable BundleRequirements Requirements;	///< system requirement fo the bundle
		mutable InterfacesProvider Interfaces;		///< supported interfaces
		mutable MethodsProvider Methods;			///< Method collector
		mutable PropertiesProvider Properties;		///< Properties collector
		mutable SecurityTokenProvider* Credentials;

	private:
		BundleGeneric(const BundleGeneric&) : Bundle() { }
		BundleGeneric& operator=(const BundleGeneric&) { return *this; }


}; // class def

}; // namespace wosh


#endif //__WOSH_Core_BundleGeneric_H__
